<template>
<div>
    <div class="common-level-rail">
      <el-button size="small" type="primary" @click="addCategory"
        >{{$t('plus.add_category')}}</el-button
      >
    </div>
    <div class="table-wrap">
      <el-table :data="categoryData" style="width: 100%" v-loading="loading">
        <el-table-column
          prop="category_id"
          :label="$t('plus.category_id')"
          width="180"
        ></el-table-column>
        <el-table-column prop="name" :label="$t('plus.category_name')"></el-table-column>
        <el-table-column prop="sort" :label="$t('plus.category_sort')"></el-table-column>
        <el-table-column
          prop="create_time"
          :label="$t('plus.create_time')"
          width="180"
        ></el-table-column>
        <el-table-column prop="name" :label="$t('plus.action')" width="150">
          <template #default="scope">
            <el-button @click="editCategory(scope.row)" type="text" size="small"
              >{{$t('plus.edit')}}</el-button
            >
            <el-button
              @click="deleteCategory(scope.row)"
              type="text"
              size="small"
              >{{$t('plus.delete')}}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--添加-->
      <Add
        v-if="open_add"
        :open_add="open_add"
        @closeDialog="closeDialogFunc($event, 'add')"
      ></Add>

      <!--编辑-->
      <Edit
        v-if="open_edit"
        :open_edit="open_edit"
        :form="userModel"
        @closeDialog="closeDialogFunc($event, 'edit')"
      ></Edit>
    </div>
  </div>
</template>

<script>
import ArticleApi from "@/api/article.js";
import Edit from "./part/Edit.vue";
import Add from "./part/Add.vue";
import { deepClone } from "@/utils/base.js";
export default {
  components: {
    /*编辑组件*/
    Edit,
    Add
  },
  data() {
    return {
      /*分类*/
      categoryData: [],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      commentData: [],
      /*是否加载完成*/
      loading: true
    };
  },
  created() {
    /*获取文章列表*/
    this.getTableList();
  },
  methods: {
    /*获取文章列表*/
    getTableList() {
      let self = this;
      let Params = {};
      ArticleApi.getCategory(Params, true).then(data => {
        self.loading = false;
        self.categoryData = data.data.catgory;
      }).catch(error => {});
    },
    /*删除文章分类*/
    deleteCategory(row) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        ArticleApi.deleteCategory({
          category_id: row.category_id
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: "success"
          });
          self.getTableList();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    },
    handleClick(tab, event) {},
    /*打开分类添加*/
    addCategory() {
      this.open_add = true;
    },
    /*打开分类编辑*/
    editCategory(item) {
      //                this.userModel = item;
      this.userModel = deepClone(item);
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    }
  }
};
</script>

